package com.alibaba.algo.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ThreadPoolUtil {

    private static final int CORE_POOL_SIZE = 50;
    private static final int MAXIMUM_POOL_SIZE = 300;
    private volatile static ExecutorService executorService = null;

    public static ExecutorService getThreadPool() {
        if (null == executorService) {
            synchronized (ThreadPoolUtil.class) {
                if (null == executorService) {
                    executorService = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE,
                            0L, TimeUnit.MILLISECONDS,
                            new LinkedBlockingQueue<Runnable>());
                    log.info("init executorService finished");
                }
            }
        }
        return executorService;
    }
}
